//
//  SMBRadioPickerView.m
//  SMBPickerView
//
//  Created by Zongzhu on 16/4/11.
//  Copyright © 2016年 Zongzhu. All rights reserved.
//

#import "SMBRadioPickerView.h"

@interface SMBRadioPickerView () <UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, copy) NSArray *strings;
@property (nonatomic, copy) NSArray *objects;
@end

@implementation SMBRadioPickerView
@dynamic pickerView;

+ (void)showPickerViewWithStrings:(NSArray *)strings completion:(void (^)(NSString *selectString))completion{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    SMBRadioPickerView *pickerView = [[self alloc] initWithFrame:CGRectZero];
    pickerView.strings = strings;
    [pickerView setPickerHidden:NO callBack:nil]; //显示
    pickerView.onDismissCompletion = completion;
    [self.rootView addSubview:pickerView];
}
+ (void)showPickerViewWithObjects:(NSArray *)objects showKeyPath:(NSString *)keypath completion:(void (^)(NSObject *selectedObject))completion{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    SMBRadioPickerView *pickerView = [[self alloc] initWithFrame:CGRectZero];
    pickerView.objects = objects;
    NSMutableArray *strings = [NSMutableArray array];
    [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [strings addObject:[obj valueForKey:keypath]];
    }];
    pickerView.strings = strings;
    [pickerView setPickerHidden:NO callBack:nil]; //显示
    pickerView.onDismissCompletion = completion;
    [self.rootView addSubview:pickerView];
}
#pragma mark -
- (id)pickerViewWithRect:(CGRect)rect{
    self.pickerView = [[UIPickerView alloc] initWithFrame:rect];
    self.pickerView.delegate = self;
    self.pickerView.dataSource = self;
    self.pickerView.showsSelectionIndicator = YES;
    self.pickerView.backgroundColor = [UIColor whiteColor];
    return self.pickerView;
}
- (void)initializePickerView{
    [self.pickerView selectRow:0 inComponent:0 animated:YES];
    if (self.objects.count > 0) {
        self.selectedObject = self.objects[0];
        return;
    }
    self.selectedObject = self.strings[0];
}
#pragma mark - UIPickerViewDataSource

- (NSInteger)numberOfComponentsInPickerView: (UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent: (NSInteger)component {
    return self.strings.count;
}

- (NSString *)pickerView: (UIPickerView *)pickerView
             titleForRow: (NSInteger)row
            forComponent: (NSInteger)component {
    return self.strings[row];
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}
#pragma mark - UIPickerViewDelegate

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (self.objects.count > 0) {
        self.selectedObject = self.objects[row];
        return;
    }
    self.selectedObject = self.strings[row];
}

@end
